package com.tantou.java.struct.queue;

public class NewQueue {

    private String[] items;

    private int n;

    private int head;
    private int tail;

    public NewQueue(int n) {
        this.n = n;
        items = new String[n];
        this.head = 0;
        this.tail = 0;
    }

    public boolean enqueue(String item) {
        if (item == null) return false;
        if (tail == n) {
            if(head==0)return false;
            for (int i = head; i < tail; i++) {
                items[i - head] = items[i];
            }
            tail -= head;
            head = 0;
        }
        items[tail] = item;
        tail++;
        return true;
    }

    public String dequeue() {
        if (tail == head) return null;
        String tmp = items[head];
        items[head] = null;
        head++;
        return tmp;
    }
}

